package com.leo.offer;

/**
 * 模拟实现Pow
 * @author xuexiaolei
 * @version 2018年03月01日
 */
public class T11 {
    public static double pow(double base, int n){
        if (base == 0 && n<=0){ throw new RuntimeException(); }
        boolean isNeg = false;
        if (n < 0){ isNeg=true; n=-n; }
        double res = power01(base, n);
        if (isNeg){
            return 1.0/res;
        }else {
            return res;
        }
    }

    private static double power01(double base, int n){
        if (n == 0){ return 1;}
        if (n == 1){ return base; }
        double res = power01(base, n>>1);
        res *= res;
        if ( (n&1)==1 ){ res*=base; }
        return res;
    }
}
